package org.glut.wechat.addresslist.controller;

import jakarta.annotation.Resource;
import org.glut.wechat.addresslist.common.Result;
import org.glut.wechat.addresslist.entity.Request;
import org.glut.wechat.addresslist.service.IRequestService;
import org.glut.wechat.addresslist.vo.RequestVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 申请添加好友表 前端控制器
 *
 * @author harry
 * @since 2025-08-21 20:25
 */
@RestController
@RequestMapping("/friend")
@CrossOrigin(origins = "http://localhost:5173")
public class RequestController {

    @Resource
    private IRequestService requestService;

    @PostMapping("/request")
    public Result<?> saveRequest(@RequestBody Request request) {
        request.setStatus(0);
        boolean success = requestService.save(request);
        return success ? Result.ok("申请发送成功") : Result.fail("申请发送失败");
    }
    // 获取待处理申请
    @GetMapping("/requests")
    public Result<List<RequestVO>> getNewRequests(@RequestParam Integer toUserId) {
        List<RequestVO> requests = requestService.getNewRequests(toUserId);
        return Result.ok(requests);
    }

    // 同意申请更新好友表
    @PutMapping("/agree")
    public Result<Boolean> agreeRequest(@RequestBody Map<String, Integer> params) {
        Integer requestId = params.get("requestId");
        boolean success = requestService.agreeRequest(requestId);
        return success ? Result.ok(true) : Result.fail("同意失败");
    }

    // 忽略申请更新状态为2
    @PutMapping("/ignore")
    public Result<Boolean> ignoreRequest(@RequestBody Map<String, Integer> params) {
        Integer requestId = params.get("requestId");
        boolean success = requestService.ignoreRequest(requestId);
        return success ? Result.ok(true) : Result.fail("忽略失败");
    }
}